package com.sgame.smobigame;

import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.OrientationEventListener;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;

public class MainActivity extends TabActivity {

	protected String TAG = "MainActivity";

	private TabHost mTabHost;
	private Resources mResources;
	private Intent intent;
	private FrameLayout mContent;	
	private OrientationEventListener myOrientationEventListener;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.activity_main);
	
		// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
		// R.layout.mm_title);
		// use for landscape mode

		mTabHost = getTabHost();
		mResources = getResources();
		 Configuration cfg = mResources.getConfiguration();
		 boolean hor = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE;
		
		 if (hor) {
		 TabWidget tw = mTabHost.getTabWidget();
		 tw.setOrientation(LinearLayout.VERTICAL);
		 }

		intiTab();
		// mCurrTab = R.id.main_tab_profile;

		// xmContent = (FrameLayout)
		// mTabHost.findViewById(android.R.id.tabcontent);
//
//		myOrientationEventListener = new OrientationEventListener(this,
//				SensorManager.SENSOR_DELAY_NORMAL) {
//			@Override
//			public void onOrientationChanged(int arg0) {
//				// TODO Auto-generated method stub
//
//			}
//		};
//
//		if (myOrientationEventListener.canDetectOrientation()) {
//			myOrientationEventListener.enable();
//		}
//		try {
//			Mobage mobage = Mobage.getInstance();
			// ssetContentView(R.layout.activity_main);
			// RadioButton v = (RadioButton) mTabHost.findViewById(mCurrTab);
			// v.setChecked(true);
			// onClickTab(v);
			/*
			 * if (! Mobage.isInitialized()) { //throw new Exception(
			 * "Please replace the following with your own values and comment out this line"
			 * ); mobage.initialize(this, ServerMode.SANDBOX,
			 * "YOUR_APP_KEY_HERE", "YOUR_VERSION_HERE",
			 * "YOUR_CONSUMER_KEY_HERE", "YOUR_CONSUMER_SECRET_HERE");
			 * 
			 * //analyticsReporter =
			 * mobage.newAnalyticsActivity(getComponentName
			 * ().flattenToString()); }
			 * 
			 * Service.executeLogin(this, new IExecuteLoginCallback() { public
			 * void onComplete(CancelableAPIStatus status, Error error) { switch
			 * (status) { case cancel: Log.i(TAG, "User cancelled login");
			 * break; case error: Log.i(TAG, "The login process had an error " +
			 * error.getDescription()); break; case success: // start your game
			 * setContentView(R.layout.activity_main); RadioButton v =
			 * (RadioButton)mTabHost.findViewById(mCurrTab); v.setChecked(true);
			 * onClickTab(v);
			 * 
			 * // add Mobage button //mobageButton = new MobageButton(myself);
			 * //contentLayout = (RelativeLayout)
			 * findViewById(R.id.RelativeLayout1);
			 * //contentLayout.addView(mobageButton);
			 * 
			 * People.getCurrentUser(new People.IGetCurrentUserCallback() {
			 * public void onComplete(SimpleAPIStatus status, Error error, User
			 * user) { switch (status) { case error: Log.i(TAG,
			 * "The getCurrentUserCall experienced an error " +
			 * error.getDescription()); break; case success: Toast toast =
			 * Toast.makeText( getApplicationContext(), user.getNickname() +
			 * ", welcome to Mobage", Toast.LENGTH_LONG); toast.show(); break; }
			 * } }); break; } } });
			 */
//
//		} catch (Exception e) {
//			Log.e(TAG, e.getMessage());
//		}
//
	}

	// them tabhost
	private void addTab(String tabName, int bgId, Intent intent) {

		TabSpec spec = mTabHost.newTabSpec(tabName);

		Drawable tabIndicator = mResources.getDrawable(bgId);
		spec.setIndicator("", tabIndicator);

		if (intent != null) {
			spec.setContent(intent);
			mTabHost.addTab(spec);
		}

	}

	// khoi tao tabhost
	private void intiTab() {
		
		
		intent = new Intent(this, ProfileView.class);
		addTab("Profile", R.drawable.tab_profile, intent);
		
		intent = new Intent(this, GamesView.class);
		addTab("Profile", R.drawable.tab_games, intent);
		
		intent = new Intent(this, FriendsView.class);
		addTab("Profile", R.drawable.tab_friends, intent);
		
		intent = new Intent(this, MyCoinsView.class);
		addTab("Profile", R.drawable.tab_mycoins, intent);
	}



	 @Override
	 public boolean onCreateOptionsMenu(Menu menu) {
	 // Inflate the menu; this adds items to the action bar if it is present.
	 getMenuInflater().inflate(R.menu.activity_main, menu);
	 return true;
	 }



	@Override
	public void onPause() {
		super.onPause();
		// if (Mobage.isInitialized()) {
		// Mobage.getInstance().onPause();
		// if (analyticsReporter != null)
		// analyticsReporter.onPause();
		// }
	}

	@Override
	public void onResume() {
		super.onResume();
		// if (Mobage.isInitialized()) {
		// Mobage.getInstance().onResume();
		// if (analyticsReporter != null)
		// analyticsReporter.onResume();
		// }
	}

	@Override
	public void onStart() {
		super.onStart();
		// if (analyticsReporter != null)
		// analyticsReporter.onStart();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// if (Mobage.isInitialized()) {
		// Mobage.getInstance().onDestroy();
		// }
	}

	 @Override
	 public boolean onOptionsItemSelected(MenuItem item) {
	 // Handle item selection
	 int i;
	 switch (item.getItemId()) {
	 case R.id.menu_settings:
	 i = mTabHost.getCurrentTab();
	 return true;
	 case R.id.menu_quit:
	 finish();
	 System.exit(0);
	 return true;
	 default:
	 return super.onOptionsItemSelected(item);
	 }
	 }
}
